package com.example.andoridclient;

import global.Global;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

public class LoginActivity extends Activity implements OnClickListener {

	private static final int USERNAME_PASSWORD_EMPTY = 2;
	private static final int SUCCESS = 3;
	private static final int LOADING = -1;
	private static final int UNLOADING= -2;
	Button button_login;
	Button button_register;

	EditText text_usrname;
	EditText text_pwd;

	ProgressBar p;

	List<NameValuePair> loginparamsList;
	
	ProgressDialog progressDialog;
	public void onClick(View v) {
		switch (v.getId()) {
		// 登陆
		case R.id.buttonlogin:
			
			 if (TextUtils.isEmpty(text_usrname.getText())
			 || TextUtils.isEmpty(text_pwd.getText())) {
			 handler.sendEmptyMessage(USERNAME_PASSWORD_EMPTY);
			 return;
			 }
			 loginparamsList = new ArrayList<NameValuePair>();
			 loginparamsList.add(new BasicNameValuePair("Username",
			 text_usrname
			 .getText().toString()));
			 loginparamsList.add(new BasicNameValuePair("Password",
			 text_pwd.getText()
			 .toString()));
			 new Thread(runnable).start();
//			Global.user.Role = "Sendout";
//			handler.sendEmptyMessage(SUCCESS);
			break;
		// 注册
		case R.id.buttonRegisterPage:
			Intent intent1 = new Intent(LoginActivity.this,
					RegisterActivity.class);
			startActivity(intent1);
			break;
		default:
			break;
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		
		button_login = (Button) findViewById(R.id.buttonlogin);
		button_register = (Button) findViewById(R.id.buttonRegisterPage);
		text_usrname = (EditText) findViewById(R.id.editTextusrname);
		text_pwd = (EditText) findViewById(R.id.editTextpwd);
		
		button_login.setOnClickListener(this);
		button_register.setOnClickListener(this);
	}

	// 线程
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			String result = "";
			try {
				handler.sendEmptyMessage(LOADING);
				result = Global.POST("Login/Login", loginparamsList);
				JSONObject usrjson = new JSONObject(result);

				// 失败
				if (usrjson.has("error")) {
					if (!TextUtils.isEmpty(usrjson.get("error").toString())) {
						Message message = new Message();
						message.obj = usrjson.get("error").toString();
						handler.sendMessage(message);
						return;
					}
				}
				else {
					Global.user.ID = usrjson.getInt("ID");
					Global.user.PhoneNumber = usrjson.getString("PhoneNumber");
					Global.user.Realname = usrjson.getString("RealName");
					Global.user.Role = usrjson.getString("Role");
					Global.user.Sex = usrjson.getString("Sex");
					Global.user.UserName = usrjson.getString("UserName");
					handler.sendEmptyMessage(SUCCESS);
				}
			} catch (JSONException e) {
				Message message = new Message();
				message.obj = result;
				handler.sendMessage(message);
			}
			finally
			{
				handler.sendEmptyMessage(UNLOADING);
			}
		}
	};
	// UI Handler
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			// 用户名密码不能为空
			case USERNAME_PASSWORD_EMPTY:
				new AlertDialog.Builder(LoginActivity.this).setTitle("提示")
						.setMessage("用户名或密码不能为空")
						.setIcon(R.drawable.ic_launcher)
						.setPositiveButton("确定", null).create().show();
				break;
			// 成功
			case SUCCESS:
				Intent intent1 = new Intent(LoginActivity.this,
						MainActivity.class);
				startActivity(intent1);
				break;

			case LOADING:
				progressDialog = ProgressDialog.show(LoginActivity.this, "请稍等...", "读取中...", true);
				break;
			case UNLOADING:
				progressDialog.dismiss();
				break;
			// 其他异常
			default:
				new AlertDialog.Builder(LoginActivity.this).setTitle("提示")
						.setMessage(msg.obj.toString())
						.setIcon(R.drawable.ic_launcher)
						.setPositiveButton("确定", null).create().show();
				break;
			}
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
